package cn.iocoder.yudao.module.member.controller.admin.invitationcode;

import cn.iocoder.yudao.module.member.controller.admin.invitationcode.vo.InvitationCodePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.invitationcode.vo.InvitationCodeRespVO;
import cn.iocoder.yudao.module.member.controller.admin.invitationcode.vo.InvitationCodeSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.invitationcode.InvitationCodeDO;
import cn.iocoder.yudao.module.member.service.invitationcode.InvitationCodeService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 邀请码")
@RestController
@RequestMapping("/member/invitation-code")
@Validated
public class InvitationCodeController {

    @Resource
    private InvitationCodeService invitationCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建邀请码")
    public CommonResult createInvitationCode(@Valid @RequestBody InvitationCodeSaveReqVO createReqVO) {
        invitationCodeService.createInvitationCode(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新邀请码")
    public CommonResult<Boolean> updateInvitationCode(@Valid @RequestBody InvitationCodeSaveReqVO updateReqVO) {
        invitationCodeService.updateInvitationCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邀请码")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteInvitationCode(@RequestParam("id") Long id) {
        invitationCodeService.deleteInvitationCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邀请码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<InvitationCodeRespVO> getInvitationCode(@RequestParam("id") Long id) {
        InvitationCodeDO invitationCode = invitationCodeService.getInvitationCode(id);
        return success(BeanUtils.toBean(invitationCode, InvitationCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邀请码分页")
    public CommonResult<PageResult<InvitationCodeRespVO>> getInvitationCodePage(@Valid InvitationCodePageReqVO pageReqVO) {
        PageResult<InvitationCodeDO> pageResult = invitationCodeService.getInvitationCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InvitationCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邀请码 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInvitationCodeExcel(@Valid InvitationCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InvitationCodeDO> list = invitationCodeService.getInvitationCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "邀请码.xls", "数据", InvitationCodeRespVO.class,
                        BeanUtils.toBean(list, InvitationCodeRespVO.class));
    }

}